package com.zhangye.test.api;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhangye.test.model.Admin;
import com.zhangye.test.model.ScanCount;
import com.zhangye.test.service.AdminService;
import com.zhangye.test.service.SafeSessionService;
import com.zhangye.test.service.ScanCountService;


/**
 * 添加扫描记录
 * 作者： 张叶
 * 创建时间：2018年6月14日  上午11:17:28
 */
@Controller
@RequestMapping("api/scanCount")
public class ScanCountApi {
	
	@Autowired
	private ScanCountService scanCountService;
	
	@Autowired
	private SafeSessionService safeSessionService;
	
	@Autowired
	private AdminService adminService;
	
	
	/**
	 * 消费详细
	 * 作者： 张叶
	 * 创建时间：2018年8月2日  下午5:05:04
	 */
	@ResponseBody
	@RequestMapping(value = "/detail", method = RequestMethod.POST)
	public ApiResult detail(int easyArId ) {
		ApiResult result = new ApiResult();
		if(easyArId != 0 ){
			String where = "   and video_url = (select video_url from easy_ar where id = " + easyArId 
					 + ") and DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= DATE(created_at)";
			List<ScanCount> list = scanCountService.selectByWhere(where);
			if(list.size() > 0){
				result.data.put("easyAr", list);
				result.msg.status = 0;
				result.msg.desc = "获取成功";
			}else{
				result.msg.status = 1;
				result.msg.desc = "暂无数据";
			}
		}else{
			result.msg.status = 3;
			result.msg.desc = "参数不能为空";
		}
		
		return result;
	}
	
	
	/**
	 * 七天折线图
	 * 作者： 张叶
	 * 创建时间：2018年7月30日  下午3:34:35
	 */
	@ResponseBody
	@RequestMapping(value = "/scanCount", method = RequestMethod.POST)
	public ApiResult scanCount(int easyArId ) {
		ApiResult result = new ApiResult();
		
		if(easyArId != 0 ){
			String where = " and video_url = (select video_url from easy_ar where id = " + easyArId 
					+ ") and DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= DATE(created_at) GROUP BY TO_DAYS(created_at)";
			List<Map<String,Object>> list = scanCountService.selectByCounts(where);
			if(list.size() > 0){
				result.data.put("easyAr", list);
				result.msg.status = 0;
				result.msg.desc = "获取成功";
			}else{
				result.msg.status = 1;
				result.msg.desc = "暂无数据";
			}
		}else{
			result.msg.status = 3;
			result.msg.desc = "参数不能为空";
		}
		
		return result;
	}
	
	
	/**
	 * 添加扫描
	 * 作者： 张叶
	 * 创建时间：2018年7月27日  上午11:17:20
	 */
	@ResponseBody
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public ApiResult insert(HttpServletRequest request,String videoUrl,
			String phoneType,String address ) {
		ApiResult result = new ApiResult();
		
		if(phoneType != null && !"".equals(phoneType)
				&& address != null && !"".equals(address)){
			ScanCount sc = new ScanCount();
			Admin admin = safeSessionService.getCurrent(request);
			if(admin != null && !"".equals(admin)){
				sc.setAdminId(admin.getId());
			}
			sc.setVideoUrl(videoUrl);
			sc.setAddress(address);
			sc.setPhoneType(phoneType);
			sc.setCreatedAt(new Date());
			if(scanCountService.insert1(sc) > 0){
				result.msg.status = 0;
				result.msg.desc = "添加成功";
			}else{
				result.msg.status = 1;
				result.msg.desc = "系统错误，添加失败";
			}
		}else{
			result.msg.status = 3;
			result.msg.desc = "参数不能为空";
		}
		
		return result;
	}
	
}
